<?php


    // 干扰象素
    function _rndPixels(){ //加入干扰象素
        for($i=0;$i<200;$i++){  
           imagesetpixel($this->im, rand()%160 , rand()%100 , $this->rndColor());    
        }
    }
    // 干扰线
    function _rndLines(){ 
        $w = $this->imw-2;
        $h = $this->imh-2;
        $cnt = mt_rand(5,8);
        for($j=2;$j<$cnt;$j++){
            imageline($this->im, rand(2,$w),rand(2,$h),rand(2,$w),rand(2,$h), $this->rndColor()
            ); //18
        }
        imagerectangle($this->im, 0, 0, $this->imw-1, $this->imh-1, $this->rndColor()); // 边框 
    }

    // 干扰字符
    function _rndChars(){ 
        if(empty($this->ttf)){ return; }
        $cnt = mt_rand(5,10); //《!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~》// All:32 '+','-',
        $chars = array('!',"'",'(',')',',','/',";","?","^","`","~");
        for($i=1; $i<=$cnt; $i++){
            imagestring($this->im,mt_rand(1,5),mt_rand(1,$this->imw),mt_rand(1,$this->imh),$chars[mt_rand(0,4)],$this->rndColor());
        }
    }
    // 雪花
    function _rndSnow(){  
        for ($i=0;$i<30;$i++) {
            $color = imagecolorallocate($this->im,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
            imagestring($this->im,mt_rand(2,5),mt_rand(0,$this->imw),mt_rand(0,$this->imh),'*',$color);
        }
    }

    // 扭曲文字
    public function distOrtion(){
        $rate = floor(0.08 * $this->imh);   // 扭曲程度
        $this->dis = imagecreatetruecolor($this->imw, $this->imh);
        imagefill($this->dis, 0, 0, $this->bgColor);
        for ($x = 0; $x < $this->imw; $x++) {
            for ($y = 0; $y < $this->imh; $y++) {
                $rgb = imagecolorat($this->im, $x, $y);
                if( (int)($x + sin($y / $this->imh * 2 * M_PI) * $rate) <= $this->imw && (int)($x + sin($y / $this->imh * 2 * M_PI) * $rate) >= 0 ) 
                {
                    imagesetpixel($this->dis, (int)($x + sin($y / $this->imh * 2 * M_PI - M_PI * 0.1) * $rate) , $y, $rgb);
                }
            }
        }
        $this->im = $this->dis;
    }
